package com.wwwc.index.web.servlet;

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletRequestWrapper;
import java.net.*;

import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;

import com.wwwc.index.web.ejb.directory.*;
import com.wwwc.util.web.*;

public class SessionBeanEntityBeanServlet extends HttpServlet {
    private DirectorySessionBeanRemote  dsbr;

    public void doPost(HttpServletRequest request, HttpServletResponse response)
    {
       	response.setContentType("text/html");
	PrintWriter out = null;

	try {
	    out = response.getWriter();
	}
	catch(Exception e) {
	}

	ArrayList alist = new ArrayList();

	if (dsbr == null) {
	    try {
		InitialContext initial = new InitialContext();
		Object objref = initial.lookup("java:comp/env/ejb/DirectorySessionEJBRef");
		DirectorySessionBeanHome sbhome = null;  
		sbhome = (DirectorySessionBeanHome)PortableRemoteObject.narrow(objref,DirectorySessionBeanHome.class);
		dsbr = (DirectorySessionBeanRemote)sbhome.create();
		
		alist = dsbr.getDirectoryList();
		//out.println("alist="+alist);
	    } 
	    catch (RemoteException ex) {
		out.println("Error: create SideBarBean1."  + ex.getMessage());
	    }
	    catch (CreateException ex) {
		out.println("Error: create SideBarBean2."  + ex.getMessage());
	    } 
	    catch (NamingException ex) {
		out.println("Error: jndi lookup home=" + ex.getMessage());
	    }
	}
	else {
	    try {
		alist = dsbr.getDirectoryList();
	    } 
	    catch (RemoteException ex) {
		out.println("Error: create SideBarBean1."  + ex.getMessage());
	    }
	    //out.println("alist="+alist);
	}
	
        out.println("Servlet --> SessionBean --> EntityBean --> Database<BR><BR>");  
	Iterator rit = alist.iterator();
	while (rit.hasNext()) {
	    out.println("<BR>"+rit.next());
	}
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    {
	doPost(request, response);
    }
}






